package com.asset.modules.common.utils;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

public class Calcutil {

    private static final double LOW_LIMIT = 0.03;

    /**
     * 计算固定资产净值
     * @param buyPrice
     * @param buyDate
     * @param totalyear
     * @param currentDate
     * @return
     */
    public static Double calcValue(String buyPrice,  String buyDate, int totalyear, String currentDate) throws ParseException {
        //对异常值进行判断
        if(buyPrice==null||buyDate==null||totalyear<=0||currentDate==null){
            return 0.00;
        }
        //已使用年限，保留两位小数
        Double usedYear = DateUtils.subtractBetweenDate(buyDate, currentDate);
        Double _buyPrice = Double.valueOf(buyPrice);
        Double value = _buyPrice -((Double) (_buyPrice*(1-0.03))/totalyear)*usedYear;
        if(value<=_buyPrice*LOW_LIMIT){
            value = _buyPrice*LOW_LIMIT;
        }
        return  Double.valueOf(new DecimalFormat("0.00").format(value));
    }


    /**
     * 计算盈亏
     * @param registerNum
     * @param registerMoney
     * @param checkNum
     * @return
     */
    public static double calcEarn(long registerNum, double registerMoney, long checkNum){
        Map result = new HashMap();
        long tempNum = checkNum - registerNum;

        double singleValue = registerMoney/registerNum;
        double earnValue = tempNum*singleValue;
        System.out.println(earnValue);
        DecimalFormat df=new DecimalFormat("0.00");
        earnValue = Double.valueOf(df.format(earnValue*tempNum));
        if(checkNum>=registerNum){
            return earnValue;
        }else{
            return -earnValue;
        }
    }

}
